package com.clothing.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.clothing.CommonAttributes;
import com.clothing.service.FileService;
import com.clothing.utils.MathUtils;
import com.clothing.utils.Utils;

@Service
public class FileServiceImpl implements FileService
{
    @Resource(name = "taskExecutor")
    private TaskExecutor taskExecutor;
    
    @Override
    public String upload(MultipartFile multipartFile)
    {
        String uuid = MathUtils.uuid();
        String path = System.getProperty("java.io.tmpdir") + "upload_" + uuid + ".tmp";
        File tempFile = new File(path);
        
        if (!tempFile.getParentFile().exists())
        {
            tempFile.getParentFile().mkdirs();
        }

        String ext = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
        String destPath = Utils.getImageUploadPath() + uuid + "." + ext;
        
        try
        {
            multipartFile.transferTo(tempFile);
            
            taskExecutor.execute(new Runnable()
            {
                public void run()
                {
                    try
                    {
                        File destFile = new File(CommonAttributes.BASE_UPLOAD_PATH + destPath);
                        FileUtils.moveFile(tempFile, destFile);
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    } 
                    finally
                    {
                        FileUtils.deleteQuietly(tempFile);
                    }
                }
            });
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
        return CommonAttributes.SITE_URL + destPath;
    }

    @Override
    public void deleteFile(String path)
    {
        String prefix = CommonAttributes.SITE_URL;

        if (path.startsWith(prefix))
        {
            path = path.substring(prefix.length(), path.length());
        }

        File file = new File(CommonAttributes.BASE_UPLOAD_PATH + path);

        if (file.exists())
        {
            FileUtils.deleteQuietly(file);
        }
    }

    @Override
    public String upload(String urlStr) throws Exception
    {
        URL url = new URL(urlStr);
        URLConnection con = url.openConnection();
        InputStream is = con.getInputStream();
        byte[] bs = new byte[1024];
        int len;
        String destPath = Utils.getImageUploadPath() + MathUtils.uuid() + ".jpg";
        String filename =CommonAttributes.BASE_UPLOAD_PATH + destPath;
        OutputStream os = new FileOutputStream(filename);
        
        while ((len = is.read(bs)) != -1)
        {
            os.write(bs, 0, len);
        }
        
        os.close();
        is.close();

        return filename;
    }
}
